﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StockPoint.Admin.Member
{
    public partial class UserList : BasePage
    {
        /// <summary>
        /// 페이지 초기화.
        /// </summary>
        protected override void InitPage()
        {
            // Page Setting & Validation
            if (ValidPageSetting() == false) return;

            // Default search option

            GetList(1);
        }

        /// <summary>
        /// 검색버튼 클릭.
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                GetList(1);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        /// <summary>
        /// Page Action (Paging, Sorting).
        /// </summary>
        protected void PageAction(object sender, EventArgs e)
        {
            try
            {
                GetList(Convert.ToInt32(hfPage.Value));
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        /// <summary>
        /// 목록 검색.
        /// </summary>
        private void GetList(int nPage)
        {
            string sSql;
            string sItems = string.Empty;
            string sMethod = string.Empty;
            int nCount;

            //----- 검색조건 생성
            string sWhere = string.Empty;

            // 기간
            if (tbSStart.Text != string.Empty)
                sWhere += " AND RegDT>'" + tbSStart.Text + "'";
            if (tbSEnd.Text != string.Empty)
                sWhere += " AND RegDT<'" + Convert.ToDateTime(tbSEnd.Text).AddDays(1).ToString("yyyy-MM-dd") + "'";

            // 테스트
            if (string.IsNullOrEmpty(txtSEmail.Text) == false)
                sWhere += " AND Email='" + txtSEmail.Text + "'";

            //----- 전체레코드 조회
            int iTotalRecord;
            if (nPage == 1)
            {
                sSql = string.Format(SelCount, sWhere);
                iTotalRecord = this.cDBUtil.GetSingleInt(sSql);
                hfTotalRecord.Value = iTotalRecord.ToString();
            }
            else
            {
                iTotalRecord = Convert.ToInt32(hfTotalRecord.Value);
            }
            lblCount.Text = iTotalRecord.ToString("N0");

            //----- 페이지목록 조회
            if (iTotalRecord == 0)
            {
                rptList.DataSource = null;
                rptList.DataBind();
                lblPaging.Text = string.Empty;
            }
            else
            {
                sSql = string.Format(SelListPage, 20, nPage, sWhere);
                rptList.DataSource = this.cDBUtil.GetDataTable(sSql);
                rptList.DataBind();
                lblPaging.Text = Paging.GetPaging(20, iTotalRecord, nPage);
            }
        }

        #region " Query "

        /// <summary>
        /// SELECT - 카운트.
        /// </summary>
        private const string SelCount = @"
SELECT      COUNT(*) 
FROM        T_User 
WHERE       1=1 {0}
";

        /// <summary>
        /// SELECT - 리스트 페이징.
        /// </summary>
        public const string SelListPage = @"
SELECT		*
FROM
            (SELECT     *, Point + PointBonus AS TotalPoint  
			            , ROW_NUMBER() OVER (ORDER BY UserIdx DESC) AS RowNo
            FROM        T_User 
            WHERE       1=1 {2}) AS M
WHERE		RowNo BETWEEN ({0}*({1}-1))+1 AND {0}*{1}
";

        #endregion

    }
}